﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using CPService.Models;

namespace CPService.Controllers
{
    public class VotationsController : ApiController
    {
        private CPServiceContext db = new CPServiceContext();

        // GET: api/Votations
        public IQueryable<Votation> GetVotations()
        {
            return db.Votations;
        }

        // GET: api/Votations/5
        [ResponseType(typeof(Votation))]
        public IHttpActionResult GetVotation(int id)
        {
            Votation votation = db.Votations.Find(id);
            if (votation == null)
            {
                return NotFound();
            }

            return Ok(votation);
        }

        // PUT: api/Votations/5
        [ResponseType(typeof(void))]
        public IHttpActionResult PutVotation(int id, Votation votation)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != votation.VotationId)
            {
                return BadRequest();
            }

            db.Entry(votation).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!VotationExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }

        // POST: api/Votations
        [ResponseType(typeof(Votation))]
        public IHttpActionResult PostVotation(Votation votation)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Votations.Add(votation);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = votation.VotationId }, votation);
        }

        // DELETE: api/Votations/5
        [ResponseType(typeof(Votation))]
        public IHttpActionResult DeleteVotation(int id)
        {
            Votation votation = db.Votations.Find(id);
            if (votation == null)
            {
                return NotFound();
            }

            db.Votations.Remove(votation);
            db.SaveChanges();

            return Ok(votation);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }

        private bool VotationExists(int id)
        {
            return db.Votations.Count(e => e.VotationId == id) > 0;
        }
    }
}